//
//  HTSubjectScrollView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/4.
//

import SwiftUI

struct HTSubjectScrollView: View {
    var var_subject: HTSubject = HTSubject()
    @State private var var_selAnime: HTAnime = HTAnime()
    @State private var var_showPushToVideoDetail: Bool = false
    @State private var var_showPushToSubject: Bool = false

    var body: some View {
        ZStack {
            NavigationLink(destination: HTVideoDetailScreen(var_id: var_selAnime.var_id, var_source: "9"), isActive: $var_showPushToVideoDetail) {
                EmptyView()
            }
            NavigationLink(destination: HTSubjectScreen(var_subId: var_subject.var_id, var_subTitle: var_subject.var_name), isActive: $var_showPushToSubject) {
                EmptyView()
            }

            LazyVStack(spacing: 8) {
                HStack {
                    Text(var_subject.var_name ?? "")
                        .font(.system(size: UIDevice.ht_fixToWidth(width: 16)))
                        .fontWeight(.bold)
                        .ht_normal()
                    Spacer()

                    if var_subject.var_seeall == "1" {
                        NavigationLink {
                            HTSubjectScreen(var_subId: var_subject.var_id, var_subTitle: var_subject.var_name)
                        } label: {
                            HStack(spacing: 2) {
                                Text("See All")
                                    .font(.system(size: UIDevice.ht_fixToWidth(width: 12)))
                                    .foregroundColor(Color(hex: "#999999"))
                                Image("seeall")
                                    .resizable()
                                    .frame(width: UIDevice.ht_fixToWidth(width: 12), height: UIDevice.ht_fixToWidth(width: 12))
                            }
                        }
                    }
                }
                .padding(.horizontal, 15)

                LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3), content: {
                    ForEach(0 ..< min(6, var_subject.ht_Animes()!.count), id: \.self) { index in
                        let var_anime = var_subject.ht_Animes()![index]
                        Button {
                            var_selAnime = var_anime
                            var_showPushToVideoDetail = true
                            HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "6", var_c_id: var_anime.var_id, var_c_name: var_anime.var_title ?? ""))
                        } label: {
                            HTSubjectItem(var_anime: var_anime, var_showDUB: false)
                        }
                    }
//                    if (var_subject.var_seeall == "1") {
//                        Button {
//                            var_showPushToSubject = true
//                            HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "7", var_secname: var_subject.var_name ?? "", var_secid: var_subject.var_id ?? ""))
//                        } label: {
//                            HStack(){
//                                Image("more")
//                                    .frame(width: UIDevice.ht_fixToWidth(width: 22), height: UIDevice.ht_fixToWidth(width: 22))
//                            }
//                            .frame(maxWidth: .infinity, maxHeight: .infinity)
//                        }
//                        .background(Color.white)
//                        .frame(width: UIDevice.ht_fixToWidth(width: 108))
//                        .cornerRadius(6)
//                    }
                })
                .padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 15))

//                ScrollView(.horizontal, showsIndicators: false) {
//                    LazyHStack(spacing: 6){
//                        ForEach(0..<var_subject.ht_Animes()!.count, id: \.self) { index in
//                            let var_anime = var_subject.ht_Animes()![index]
//                            Button{
//                                var_selAnime = var_anime
//                                var_showPushToVideoDetail = true
//                                HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "6", var_c_id: var_anime.var_id, var_c_name: var_anime.var_title ?? ""))
//                            } label: {
//                                HTSubjectItem(var_anime: var_anime, var_showDUB: false)
//                            }
//                        }
//                        if (var_subject.var_seeall == "1") {
//                            Button {
//                                var_showPushToSubject = true
//                                HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "7", var_secname: var_subject.var_name ?? "", var_secid: var_subject.var_id ?? ""))
//                            } label: {
//                                HStack(){
//                                    Image("more")
//                                        .frame(width: UIDevice.ht_fixToWidth(width: 22), height: UIDevice.ht_fixToWidth(width: 22))
//                                }
//                                .frame(maxWidth: .infinity, maxHeight: .infinity)
//                            }
//                            .background(Color.white)
//                            .frame(width: UIDevice.ht_fixToWidth(width: 108))
//                            .cornerRadius(6)
//                        }
//                    }
//                    .padding(EdgeInsets(top: 0, leading: 15, bottom: 0, trailing: 15))
//                }
//                .frame(maxWidth: .infinity)
            }
            .frame(maxWidth: .infinity)
        }
        .frame(maxWidth: .infinity)
    }
}
